package com.monas.client.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.jfree.data.time.TimeSeries;

import com.monas.client.jfree.ChartValue;
import com.monas.client.util.UIHelper;
import com.monas.client.view.MainView;

public class ChartDialog extends TitleAreaDialog {

	/**
	 * Tinggi dialog.
	 */
	private static final int HEIGHT = 210;

	/**
	 * Lebar dialog.
	 */
	private static final int WIDTH = 200;

	private final List<ChartValue> values;
	private final TimeSeries series;

	private Table table;
	private int selectIndex;

	public ChartDialog(Shell parentShell, TimeSeries series,
			List<ChartValue> values, int selectIndex) {
		super(parentShell);
		this.series = series;
		this.values = values;
		this.selectIndex = selectIndex;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Monas Client " + MainView.VERSION);
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle(series.getKey().toString());
		setMessage("Detail time series that clicked");

		// hidden button cancel.
		Button cancel = getButton(IDialogConstants.CANCEL_ID);
		cancel.setEnabled(false);

		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(UIHelper.fillBoth());
		composite.setLayout(new GridLayout(1, false));

		table = new Table(composite, SWT.VIRTUAL | SWT.FULL_SELECTION
				| SWT.MULTI | SWT.V_SCROLL);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(UIHelper.fillBoth());

		UIHelper.createColumn(table, series.getDomainDescription(), 150);
		UIHelper.createColumn(table, series.getRangeDescription(), 100);

		for (int i = 0; i < values.size(); i++) {
			ChartValue value = values.get(i);
			TableItem item = new TableItem(table, SWT.None);
			String[] fields = { value.getXString(),
					value.getYValue().toString() };
			item.setText(fields);

			// beri warna biru untuk chart item yang di-click
			if (i == selectIndex) {
				item.setForeground(new Color(parent.getDisplay(), 0, 0, 255));
			}
		}

		// pilih table item sesuai dengan chart item yang di-click.
		table.setSelection(selectIndex);

		table.layout();

		return composite;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(convertHorizontalDLUsToPixels(WIDTH),
				convertVerticalDLUsToPixels(HEIGHT));
	}
}
